﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Globalization;

using Microsoft.SqlServer.Management.UI.VSIntegration;
using Microsoft.SqlServer.Management.UI.VSIntegration.ObjectExplorer;
using Microsoft.Win32;

namespace XEvents.Addin
{
    //public class AddinSettings
    //{
    //    private static readonly string addinHive = @"Software\Microsoft\Microsoft SQL Server\100\Tools\Shell\Addins\XEvents.Addin";
    //    private static RegistryKey rgKey = Registry.CurrentUser.OpenSubKey(addinHive);

    //    private static string GetAddinSetting(string keyname)
    //    {
    //        return Registry.CurrentUser.OpenSubKey(addinHive).GetValue(keyname, string.Empty).ToString();
    //    }
    //    private static void UpdateAddinSetting(string keyname, string value)
    //    {
    //        Registry.CurrentUser.OpenSubKey(addinHive, true).SetValue(keyname, value);
    //    }

    //    public static vsWindowState AddinState
    //    {
    //        get
    //        {
    //            if (!Registry.CurrentUser.OpenSubKey(addinHive).GetValueNames().Contains("AddinState"))
    //                UpdateAddinSetting("AddinState", vsWindowState.vsWindowStateNormal.ToString());
    //            return (vsWindowState)Enum.Parse(typeof(vsWindowState), GetAddinSetting("AddinState"));
    //        }
    //        set
    //        {
    //            UpdateAddinSetting("AddinState", value.ToString());
    //        }
    //    }

    //    public static bool AddinVisible
    //    {
    //        get
    //        {
    //            return Properties.Settings.Default.AddinVisible;
    //            //if (!Registry.CurrentUser.OpenSubKey(addinHive).GetValueNames().Contains("AddinVisible"))
    //            //    UpdateAddinSetting("AddinVisible", "true");
    //            //return Boolean.Parse(GetAddinSetting("AddinVisible"));
    //        }
    //        set
    //        {
    //            Properties.Settings.Default.AddinVisible = value;
    //            //UpdateAddinSetting("AddinState", value.ToString());
    //        }
    //    }

    //    public static bool CheckVersion
    //    {
    //        get
    //        {
    //            return Properties.Settings.Default.CheckVersion;
    //            if (!Registry.CurrentUser.OpenSubKey(addinHive).GetValueNames().Contains("CheckVersion"))
    //                UpdateAddinSetting("CheckVersion", "true");
    //            return Boolean.Parse(GetAddinSetting("CheckVersion"));
    //        }
    //        set
    //        {
    //            UpdateAddinSetting("CheckVersion", value.ToString());
    //        }
    //    }

    //}

    
    public class SSMSAddinHelper
    {



        // Guids available to be assigned to Tool Windows
        public static readonly string[] ToolWindowGuids = new string[10] 
                  { "0A53E064-1D90-4253-ABA0-021E3C5A7E22",
                    "DD5663C7-F4C2-4FBB-8BCE-6A1A2B89A7E8",
                    "3F5585EA-9BE6-45B8-9E93-3198B270B77D",
                    "3EA9E90F-6781-4BF7-8260-3D34D6FBBBC7",
                    "E4AC6390-E902-4C58-9D71-ACF617A2C0D3",
                    "A9BF53D0-3760-4776-BE93-BD982BF6505C",
                    "38FB1A23-F087-4AE4-B755-EDE6604D63F6",
                    "555DBB56-95B6-467E-BB9E-7A073FA8B053",
                    "88B038A2-0DC2-4D6C-B469-11DFED9E14CA",
                    "4A2F79A3-EB61-449F-9EDD-5E58C0349B3D" };

        public static Window XEventSessionExplorer
        {
            get
            {
                foreach (Window w in ServiceCache.ExtensibilityModel.Windows)
                {
                    if (w.Caption == "Extended Events Information")
                        return w;
                }
                return null;
            }
        }

        public static IObjectExplorerService ObjectExplorer
        {
            get
            {
                return (IObjectExplorerService)ServiceCache.ServiceProvider.GetService(typeof(IObjectExplorerService));
            }
        }

        public static INodeInformation ObjectExplorerSelectedNode
        {
            get
            {
                int count;
                INodeInformation[] informationArray;
                ObjectExplorer.GetSelectedNodes(out count, out informationArray);
                if (count > 0)
                {
                    return informationArray[0];
                }
                return null;
            }
        }

        public static Window GetOpenWindow(string name)
        {
            Windows wins = ServiceCache.ExtensibilityModel.Windows;
            foreach (Window w in wins)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}", w.Caption, w.ObjectKind));
            }
            return null;
        }

        public static Window CreateToolWindow(string typeName, Guid uiTypeGuid, string caption)
        {
            Windows2 win2 = (Windows2)ServiceCache.ExtensibilityModel.Windows;
            if (win2 != null)
            {
                object docObject = ServiceCache.ExtensibilityModel.ActiveDocument;
                object controlObject = ServiceCache.ExtensibilityModel.ActiveDocument;
                Assembly asm = Assembly.GetExecutingAssembly();
                //win2.CreateToolWindow(addinInstance, "XEvent.Addin", 
                try
                {
                    Window toolWindow = win2.CreateToolWindow2(CurrentAddin, Assembly.GetExecutingAssembly().Location, typeName, caption, "{" + uiTypeGuid.ToString() + "}", ref controlObject);
                    toolWindow.Visible = true;
                    toolWindow.IsFloating = false;
                    toolWindow.SetKind(vsWindowType.vsWindowTypeDesigner);

                    return toolWindow;
                }
                catch
                { }
            }
            return null;
        }

        public static Window CreateToolWindow(Guid uiTypeGuid, string caption)
        {
            Windows2 win2 = (Windows2)ServiceCache.ExtensibilityModel.Windows;
            if (win2 != null)
            {
                EnvDTE.AddIn addinInstance = CurrentAddin;

                object controlObject = ServiceCache.ExtensibilityModel.ActiveDocument;
                Assembly asm = Assembly.GetExecutingAssembly();

                Window toolWindow = win2.CreateToolWindow(addinInstance, addinInstance.ProgID, caption, "{" + uiTypeGuid.ToString() + "}", ref controlObject);
                toolWindow.Visible = true;
                return toolWindow;
            }
            return null;
        }

        public static AddIn CurrentAddin
        {
            get
            {
                foreach (AddIn a in ServiceCache.ExtensibilityModel.AddIns)
                {
                    if (a.ProgID == "XEvents.Addin")
                        return a;
                }
                return null;
            }
        }
    }
}
